package com.example.myapplication.data

import android.app.Activity
import android.app.Activity.RESULT_OK
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Bitmap
import android.provider.MediaStore
import android.widget.Toast

class photo(val packageManager: PackageManager) {
    val REQUEST_IMAGE_CAPTURE = 1 // 拍照的请求码

    fun takePhoto(activity: Activity) {
        dispatchTakePictureIntent(activity)
    }

    private fun dispatchTakePictureIntent(activity: Activity) {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takePictureIntent.resolveActivity(packageManager) != null) {
            activity.startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE)
        } else {
            // 处理没有相机应用的情况
            Toast.makeText(activity, "无法访问相机", Toast.LENGTH_SHORT).show()
        }
    }

    fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            val imageBitmap = data?.extras?.get("data") as Bitmap
            // 在这里可以处理拍照返回的照片数据 imageBitmap
        }
    }
}